package ynu.edu.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import ynu.edu.common.entity.LogicEntity;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Entity
@Getter
@Setter
public class Shop extends LogicEntity {
    @Column
    private String name;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"shops"})
    private Business business;
    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"shop"})
    private Set<ShopItem> shopItems;
    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnore
//    @JsonIgnoreProperties(value = {"shop"})
    private Set<Order1> orders;
    public ShopItem addShopItem(ShopItem shopItem){
        shopItems.add(shopItem);
        return shopItem;
    }
    public Order1 cancelOrder(Order1 order){
        for(Order1 o:orders){
            if(Objects.equals(o.getId(), order.getId())){
                o.setStatus(3);
            }
        }
        return order;
    }
    public List<Order1> findAllOrder(){
        return new ArrayList<>(orders);
    }
    public List<Order1> findOrderWithCustomer(Customer customer){
        List<Order1> order1List=new ArrayList<>();
        for(Order1 o:orders){
            if(Objects.equals(o.getCustomer().getId(), customer.getId())){
                order1List.add(o);
            }
        }
        return order1List;
    }
    public Order1 confirmOrder(Order1 order){
        for(Order1 o:orders){
            if(Objects.equals(o.getId(), order.getId())){
                o.setStatus(2);
                return o;
            }
        }
        return null;
    }
}
